.POSIX:


#
# Special targets
#

all:
	:

.PHONY: analysis check clean dist distcheck distclean docs \
        install install-comp install-man install-strip install-module \
	installdirs maintainer-clean mostlyclean sigdist sigdistcheck \
	virtinstall pre-install post-uninstall uninstall uninstall-module \
	uninstallfiles usrinstall

.SUFFIXES:


#
# Macros
#

BANDIT = bandit
BANDITARGS = -qc pyproject.toml
CTAGS = ctags
DESTDIR =
FLAKE8 = flake8
GPG = gpg -ab
GPGARGS = -q --batch --yes
SHELL = /bin/sh
INSTALL = install
MYPY = mypy
MYPYARGS = --no-error-summary
PATHCHK = pathchk
PYLINT = pylint
PYTHON = python3
PIP = $(PYTHON) -m pip
PIPARGS = --root=$(DESTDIR) --prefix=$(prefix)
PREFIX = /usr/local
RUFF = ruff
SPHINX = sphinx-build
VENV = $(PYTHON) -m venv
VERMIN = vermin
VULTURE = vulture
VPATH = .
TAR = pax -ws '/^/$(distname)\//' -x ustar
UNTAR = tar -x
UNZIP = gzcat
ZIP = gzip

bashcompdir = $(datarootdir)/bash-completion/completions
bindir = $(exec_prefix)/bin
checks = discover
datarootdir = $(prefix)/share
distar = $(distname).tgz
distfiles = \
	INSTALL.rst \
	LICENCE.rst \
	NEWS.rst \
	README.rst \
	comp/sievemgr.bash \
	comp/sievemgr.zsh \
	docs/Makefile \
	docs/_static/html/custom.css \
	docs/changelog.rst \
	docs/conf.py \
	docs/contrib.rst \
	docs/feedback.rst \
	docs/index.rst \
	docs/install.rst \
	docs/licence.rst \
	docs/module.rst \
	docs/packaging.rst \
	docs/security.rst \
	docs/sieve.cf.rst \
	docs/sievemgr.rst \
	docs/tools.txt \
	examples/sievepatch \
	linters.txt \
	makefile \
	man/sieve.cf.5 \
	man/sievemgr.1 \
	pyproject.toml \
	setup.cfg \
	sievemgr.py \
	tests/__init__.py \
	tests/test_acap.py \
	tests/test_backup.py \
	tests/test_log.py \
	tests/test_sasl.py \
	tests/test_sieve.py \
	tests/test_term.py \
	rfcs/rfc2195.txt \
	rfcs/rfc2244.txt \
	rfcs/rfc2782.txt \
	rfcs/rfc3454.txt \
	rfcs/rfc4013.txt \
	rfcs/rfc4422.txt \
	rfcs/rfc4616.txt \
	rfcs/rfc5019.txt \
	rfcs/rfc5198.txt \
	rfcs/rfc5228.txt \
	rfcs/rfc5280.txt \
	rfcs/rfc5802.txt \
	rfcs/rfc5804.txt \
	rfcs/rfc6960.txt \
	rfcs/rfc7677.txt

distname = $(module)-$(version)
exec_prefix = $(prefix)
installdirs = \
	$(DESTDIR)$(man1dir) \
	$(DESTDIR)$(man5dir) \
	$(DESTDIR)$(bashcompdir) \
	$(DESTDIR)$(zshcompdir)

mandir = $(datarootdir)/man
man1dir = $(mandir)/man1
man5dir = $(mandir)/man5
man1ext = .1
man5ext = .5
module = sievemgr
prefix = $(PREFIX)

sources = \
	sievemgr.py \
	tests/__init__.py \
	tests/test_acap.py \
	tests/test_backup.py \
	tests/test_log.py \
	tests/test_sasl.py \
	tests/test_sieve.py \
	tests/test_term.py

venvdir = /opt/odkr/sievemgr
version = 0.7.4
zshcompdir = $(datarootdir)/zsh-completions


#
# Installation
#

install: pre-install install-module install-man install-comp

install-man: \
	installdirs \
	$(DESTDIR)$(man1dir)/sievemgr$(man1ext) \
	$(DESTDIR)$(man5dir)/sieve.cf$(man5ext)

uninstall: uninstall-module uninstallfiles uninstalldirs post-uninstall

install-comp: \
	$(DESTDIR)$(bashcompdir)/sievemgr.bash \
	$(DESTDIR)$(zshcompdir)/_sievemgr

install-strip: install

virtinstall: $(venvdir)/bin/pip

install-module:
	$(NORMAL_INSTALL)
	$(PIP) install $(PIPARGS) .
	printf 'pip: %s\n' '$(PIP)' >>install.log

pre-install:
	$(PRE_INSTALL)
	set -C && : >install.log

installdirs:
	$(PRE_INSTALL)
	mkdir -pm0755 $(installdirs)
	printf 'dir: %s\n' >>install.log $(installdirs)

userinstall:
	$(MAKE) -e PIPARGS=--user prefix="$$HOME/.local" install

virtinstall:
	$(MAKE) -e PIP=$(venvdir)/bin/pip PIPARGS= PREFIX='$(venvdir)' install

uninstall-module:
	$(NORMAL_UNINSTALL)
	-"$$(sed -n 's/pip: //p' install.log)" uninstall -y $(module)

uninstalldirs:
	$(POST_UNINSTALL)
	- sed -n 's/^dir: //p' install.log | xargs -E '' rmdir -pv

uninstallfiles:
	$(POST_UNINSTALL)
	sed -n 's/^file: //p' install.log | xargs -E '' rm -f

post-uninstall:
	$(POST_UNINSTALL)
	rm install.log

$(DESTDIR)$(man1dir)/sievemgr$(man1ext): man/sievemgr.1
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sievemgr.1 '$@'
	printf 'file: %s\n' '$@' >>install.log

$(DESTDIR)$(man5dir)/sieve.cf$(man5ext): man/sieve.cf.5
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sieve.cf.5 '$@'
	printf 'file: %s\n' '$@' >>install.log

$(DESTDIR)$(bashcompdir)/sievemgr.bash: comp/sievemgr.bash
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.bash '$@'
	printf 'file: %s\n' '$@' >>install.log

$(DESTDIR)$(zshcompdir)/_sievemgr: comp/sievemgr.zsh
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.zsh '$@'
	printf 'file: %s\n' '$@' >>install.log

$(venvdir)/bin/pip:
	$(PYTHON) -mvenv $(venvdir)


#
# Documentation
#

docs: man/sievemgr.1 man/sieve.cf.5

man/sievemgr.1: docs/sievemgr.rst

man/sieve.cf.5: docs/sieve.cf.rst

man/sievemgr.1.gz: man/sievemgr.1

man/sieve.cf.5.gz: man/sieve.cf.5

man/sievemgr.1 man/sieve.cf.5:
	$(SPHINX) -Qbman docs man $@


#
# Distribution
#

dist: clean $(distar)

distcheck: dist

sigdist: dist $(distar).asc

sigdistcheck: distcheck sigdist

$(distar): $(distfiles)

$(distar).asc: $(distar)

distcheck:
	$(UNZIP) $(distar) | $(UNTAR)
	cd $(distname) && $(MAKE) -e check
	rm -rf $(distname)

$(distar):
	$(PATHCHK) $(PATHCHKARGS) -Pp $(distfiles)
	$(TAR) $(TARARGS) $(distfiles) | $(ZIP) $(ZIPARGS) >$(distar)

$(distar).asc:
	$(GPG) $(GPGARGS) $(distar)


#
# Cleanup
#

clean: mostlyclean

distclean: clean

maintainer-clean: distclean

clean:
	rm -rf .*_cache __pycache__ tests/__pycache__

distclean:
	rm -f install.log tags

maintainer-clean:
	@echo '$@ is for maintainers only!'
	@echo 'It deletes files that cannot be built with standard tools.'
	rm -f man/sievemgr.1 man/sievemgr.5.cf

mostlyclean:
	rm -rf $(distname) $(module).egg-info build
	rm -f $(distar) $(distar).asc


#
# Tags
#

tags: $(sources)
	$(CTAGS) $(CTAGSARGS) $(sources)


#
# Tests
#

check:
	@$(PYTHON) -m unittest $(checks)

quickcheck:
	@QUICKTEST=y $(PYTHON) -m unittest $(checks)


#
# Linters
#

lint: quicklint

lint:
	$(RUFF) check $(RUFFARGS) $(sources) || [ $$? -eq 127 ]
	$(FLAKE8) $(FLAKE8ARGS) $(sources) || [ $$? -eq 127 ]
	$(PYLINT) $(PYLINTARGS) $(sources) || [ $$? -eq 127 ]

quicklint:
	$(MYPY) $(MYPYARGS) $(sources) || [ $$? -eq 127 ]
	$(BANDIT) $(BANDITARGS) $(sources) || [ $$? -eq 127 ]
	$(VULTURE) $(VULTUREFLAGS) $(sources) || [ $$? -eq 127 ]
	$(VERMIN) $(VERMINARGS) $(sources) || [ $$? -eq 127 ]

